/**
 * @Copyright:Copyright (c) 1992 - 2015
 * @Company: TengTong
 */
package com.tengtong.duang.commons.utils;

import java.math.BigDecimal;
import java.math.BigInteger;

import com.tengtong.duang.commons.base.AssertUtils;
import com.tengtong.duang.commons.utils.helper.NumberTypeViewBound;
import com.tengtong.duang.commons.utils.helper.ViewBoundHelper;

/**
 * 数值工具类
 *
 * @author jai
 * @since: v1.0
 */
public class NumberUtils {

	/**
	 * 将字符串转换成 指定的 数值类型
	 *
	 * @param value
	 * @param targetClass
	 * @return
	 */
	public static <T extends Number> T stringToNumber(String value, Class<?> targetClass){
		AssertUtils.notNull(targetClass, "target Class must not be null.");
		AssertUtils.notNull(value, "Text must not be null.");
		
		if(value.trim().length() >0 && !Character.class.equals(targetClass)){
			NumberTypeViewBound bound = null;
			if(ClassUtils.isPrimitiveType(targetClass)){
				 bound = (NumberTypeViewBound)ViewBoundHelper.asIs(ClassUtils.primitiveTypeNameSet.get(targetClass), NumberTypeViewBound.class);
			}else if(ClassUtils.isPrimitiveWrapperType(targetClass)){
				if(targetClass.equals(BigInteger.class))
					return (T)new BigInteger(value);
				else if(targetClass.equals(BigDecimal.class))
					return (T)new BigDecimal(value);
				bound = (NumberTypeViewBound)ViewBoundHelper.asIs(targetClass, NumberTypeViewBound.class);
			}
			
			if(bound != null)
				return bound.valueOf(value);
		}
		return null;
	}
	
	/**
	 * 将字符串 value 转为目标类 targetClass 数值对象。
	 * 
	 * 例如 转为  int, long, double 等数据。
	 *
	 * @param value
	 * @param targetClass
	 * @return
	 */
	public static <T extends Number> T toWrapperType(String value, Class<?> targetClass){
		AssertUtils.notNull(targetClass, "target Class must not be null.");
		AssertUtils.notNull(value, "Text must not be null.");
		
		if(value == null || value.trim().length() <=0)
			return null;
		
		if(ClassUtils.isPrimitiveType(targetClass) && !Character.class.equals(targetClass)){
			NumberTypeViewBound bound = (NumberTypeViewBound)ViewBoundHelper.asIs(ClassUtils.primitiveTypeNameSet.get(targetClass), NumberTypeViewBound.class);
			return bound.valueOf(value);
		}else{
			throw new IllegalArgumentException(
					"Cannot convert String [" + value + "] to target class [" + targetClass.getName() + "]");			
		}
	}
	
	/**
	 * 将字符串 value 转成  Integer, Long, Double 等对象
	 *
	 * @param value
	 * @param targetClass
	 * @return
	 */
	public static <T extends Number> T toPrimitiveWrapperType(String value, Class<?> targetClass){
		AssertUtils.notNull(targetClass, "target Class must not be null.");
		AssertUtils.notNull(value, "Text must not be null.");
		
		if(value == null || value.trim().length() <=0)
			return null;
		
		if(ClassUtils.isPrimitiveWrapperType(targetClass) && !Character.class.equals(targetClass)){
			if(targetClass.equals(BigInteger.class))
				return (T)new BigInteger(value);
			else if(targetClass.equals(BigDecimal.class))
				return (T)new BigDecimal(value);
			
			NumberTypeViewBound bound = (NumberTypeViewBound)ViewBoundHelper.asIs(targetClass, NumberTypeViewBound.class);
			return bound.valueOf(value);
		}else{
			throw new IllegalArgumentException(
					"Cannot convert String [" + value + "] to target class [" + targetClass.getName() + "]");			
		}
	}	
}
